home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / stevie 3.10 / unix.c < prev    next >
Text File  |  1991-01-03  |  3KB  |  265 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/unix.c,v 1.8 89/08/06 09:51:13 tony Exp $
  2.  *
  3.  * System-dependent routines for UNIX System V or Berkeley.
  4.  */
  5.  
  6. #include "stevie.h"
  7. #ifdef BSD
  8. #include <sgtty.h>
  9. #else
  10. #include <termio.h>
  11. #endif
  12. #include <signal.h>
  13.  
  14. /*
  15.  * inchar() - get a character from the keyboard
  16.  */
  17. int
  18. inchar()
  19. {
  20.     char    c;
  21.  
  22.     flushbuf();        /* flush any pending output */
  23.  
  24.     do {
  25.         while (read(0, &c, 1) != 1)
  26.             ;
  27.     } while (c == NUL);
  28.  
  29.     got_int = FALSE;
  30.     return c;
  31. }
  32.  
  33. #define    BSIZE    2048
  34. static    char    outbuf[BSIZE];
  35. static    int    bpos = 0;
  36.  
  37. void
  38. flushbuf()
  39. {
  40.     if (bpos != 0)
  41.         write(1, outbuf, bpos);
  42.     bpos = 0;
  43. }
  44.  
  45. /*
  46.  * Macro to output a character. Used within this file for speed.
  47.  */
  48. #define    outone(c)    outbuf[bpos++] = c; if (bpos >= BSIZE) flushbuf()
  49.  
  50. /*
  51.  * Function version for use outside this file.
  52.  */
  53. void
  54. outchar(c)
  55. char    c;
  56. {
  57.     outone(c);
  58. }
  59.  
  60. void
  61. outstr(s)
  62. register char    *s;
  63. {
  64.     while (*s) {
  65.         outone(*s++);
  66.     }
  67. }
  68.  
  69. void
  70. beep()
  71. {
  72.     outone('\007');
  73. }
  74.  
  75. /*
  76.  * remove(file) - remove a file
  77.  */
  78. void
  79. remove(file)
  80. char *file;
  81. {
  82.     unlink(file);
  83. }
  84.  
  85. /*
  86.  * rename(of, nf) - rename existing file 'of' to 'nf'
  87.  */
  88. void
  89. rename(of, nf)
  90. char    *of, *nf;
  91. {
  92.     unlink(nf);
  93.     link(of, nf);
  94.     unlink(of);
  95. }
  96.  
  97. void
  98. delay()
  99. {
  100.     /* not implemented */
  101. }
  102.  
  103. #ifdef BSD
  104. static    struct    sgttyb    ostate;
  105. #else
  106. static    struct    termio    ostate;
  107. #endif
  108.  
  109. /*
  110.  * Go into cbreak mode
  111.  */
  112. void
  113. set_tty()
  114. {
  115. #ifdef BSD
  116.     struct    sgttyb    nstate;
  117.  
  118.     ioctl(0, TIOCGETP, &ostate);
  119.     nstate = ostate;
  120.     nstate.sg_flags &= ~(XTABS|CRMOD|ECHO);
  121.     nstate.sg_flags |= CBREAK;
  122.     ioctl(0, TIOCSETN, &nstate);
  123. #else
  124.     struct    termio    nstate;
  125.  
  126.     ioctl(0, TCGETA, &ostate);
  127.     nstate = ostate;
  128.     nstate.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
  129.     nstate.c_cc[VMIN] = 1;
  130.     nstate.c_cc[VTIME] = 0;
  131.     ioctl(0, TCSETAW, &nstate);
  132. #endif
  133. }
  134.  
  135. /*
  136.  * Restore original terminal modes
  137.  */
  138. void
  139. reset_tty()
  140. {
  141. #ifdef BSD
  142.     ioctl(0, TIOCSETP, &ostate);
  143. #else
  144.     ioctl(0, TCSETAW, &ostate);
  145. #endif
  146. }
  147.  
  148. void
  149. sig()
  150. {
  151.     signal(SIGINT, sig);
  152.     signal(SIGQUIT, sig);
  153.  
  154.     got_int = TRUE;
  155. }
  156.  
  157. void
  158. windinit()
  159. {
  160. #ifdef    TERMCAP
  161.     if (t_init() != 1) {
  162.         fprintf(stderr, "unknown terminal type\n");
  163.         exit(1);
  164.     }
  165. #else
  166.     Columns = 80;
  167.     P(P_LI) = Rows = 24;
  168. #endif
  169.  
  170.     /*
  171.      * The code here makes sure that there isn't a window during which
  172.      * we could get interrupted and exit with the tty in a weird state.
  173.      */
  174.     signal(SIGINT, sig);
  175.     signal(SIGQUIT, sig);
  176.  
  177.     set_tty();
  178.  
  179.     if (got_int)
  180.         windexit(0);
  181. }
  182.  
  183. void
  184. windexit(r)
  185. int r;
  186. {
  187.     reset_tty();
  188.     exit(r);
  189. }
  190.  
  191. void
  192. windgoto(r, c)
  193. register int    r, c;
  194. {
  195. #ifdef    TERMCAP
  196.     char    *tgoto();
  197. #else
  198.     r += 1;
  199.     c += 1;
  200. #endif
  201.  
  202.     /*
  203.      * Check for overflow once, to save time.
  204.      */
  205.     if (bpos + 8 >= BSIZE)
  206.         flushbuf();
  207.  
  208. #ifdef    TERMCAP
  209.     outstr(tgoto(T_CM, c, r));
  210. #else
  211.     outbuf[bpos++] = '\033';
  212.     outbuf[bpos++] = '[';
  213.     if (r >= 10)
  214.         outbuf[bpos++] = r/10 + '0';
  215.     outbuf[bpos++] = r%10 + '0';
  216.     outbuf[bpos++] = ';';
  217.     if (c >= 10)
  218.         outbuf[bpos++] = c/10 + '0';
  219.     outbuf[bpos++] = c%10 + '0';
  220.     outbuf[bpos++] = 'H';
  221. #endif
  222. }
  223.  
  224. FILE *
  225. fopenb(fname, mode)
  226. char    *fname;
  227. char    *mode;
  228. {
  229.     return fopen(fname, mode);
  230. }
  231.  
  232. char *
  233. fixname(s)
  234. char    *s;
  235. {
  236.     return s;
  237. }
  238.  
  239. /*
  240.  * doshell() - run a command or an interactive shell
  241.  */
  242. void
  243. doshell(cmd)
  244. char    *cmd;
  245. {
  246.     char    *getenv();
  247.     char    cline[128];
  248.  
  249.     outstr("\r\n");
  250.     flushbuf();
  251.  
  252.     if (cmd == NULL) {
  253.         if ((cmd = getenv("SHELL")) == NULL)
  254.             cmd = "/bin/sh";
  255.         sprintf(cline, "%s -i", cmd);
  256.         cmd = cline;
  257.     }
  258.  
  259.     reset_tty();
  260.     system(cmd);
  261.     set_tty();
  262.  
  263.     wait_return();
  264. }
  265.